package com.dinstar.dinlink.weiget.floatingwindow

import android.annotation.SuppressLint
import android.app.Activity
import android.app.WallpaperManager
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.PowerManager
import android.provider.Settings
import android.view.WindowManager
import android.widget.ImageView
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.dinstar.dinlink.R
import com.dinstar.dinlink.server.VoiceFloatingService

/**
 *  仿语音聊天页面，当前页面存在时不显示悬浮窗，不存在时则显示悬浮窗
 */
class VoiceActivity : Activity() {
    private var mIsClose = false


    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        //获取电源管理器对象
        val pm = this.getSystemService(POWER_SERVICE) as PowerManager
        if (!pm.isScreenOn) {
            @SuppressLint("InvalidWakeLockTag") val wl = pm.newWakeLock(
                PowerManager.ACQUIRE_CAUSES_WAKEUP or
                        PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright"
            )
            wl.acquire() //点亮屏幕
            wl.release() //任务结束后释放
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //四个标志位顾名思义，分别是锁屏状态下显示，解锁，保持屏幕长亮，打开屏幕。这样当Activity启动的时候，它会解锁并亮屏显示。
        val win = window
        win.addFlags(
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED //锁屏状态下显示
                    or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD //解锁
                    or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON //保持屏幕长亮
                    or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON //打开屏幕
                    or WindowManager.LayoutParams.FLAG_FULLSCREEN //设置全屏显示
        )


        // 在锁屏状态下显示 Activity
        setShowWhenLocked(true)
        // 唤醒屏幕
        setTurnScreenOn(true)

        //使用手机的背景
        val wallPaper = WallpaperManager.getInstance(this).drawable
        win.setBackgroundDrawable(wallPaper)
        setContentView(R.layout.activity_message)


        setContentView(R.layout.activity_voice)


        findViewById<ImageView>(R.id.iv_dismiss).setOnClickListener {
            //Android 6.0 以下无需获取权限，可直接展示悬浮窗
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //判断是否拥有悬浮窗权限，无则跳转悬浮窗权限授权页面
                if (Settings.canDrawOverlays(this)) {
                    showFloatingView()
                    finish()
                } else {
                    //跳转悬浮窗权限授权页面
                    startActivity(
                        Intent(
                            Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                            Uri.parse("package:$packageName")
                        )
                    )
                }
            } else {
                showFloatingView()
                finish()
            }
        }

        findViewById<ImageView>(R.id.iv_close) .setOnClickListener {
            mIsClose = true
            VoiceFloatingService.stopSelf()
            finish()
        }
    }

    override fun onResume() {
        super.onResume()
        dismissFloatingView()
    }

    override fun onPause() {
        super.onPause()
        if (!mIsClose) {
            //显示悬浮窗
            showFloatingView()
        }

    }

    //隐藏悬浮窗
    private fun dismissFloatingView(){
        if (VoiceFloatingService.isStart) {
            LocalBroadcastManager.getInstance(this).sendBroadcast(Intent(VoiceFloatingService.ACTION_DISMISS_FLOATING))
        }
    }

    /**
     * 显示悬浮窗
     */
    private fun showFloatingView() {
        if (VoiceFloatingService.isStart) {
            //通知显示悬浮窗
            LocalBroadcastManager.getInstance(this).sendBroadcast(Intent(VoiceFloatingService.ACTION_SHOW_FLOATING))
        } else {
            //启动悬浮窗管理服务
            startService(Intent(this, VoiceFloatingService::class.java))
        }
    }
}